package com.study.gamecube.controller;

import com.study.gamecube.common.R;
import com.study.gamecube.controller.dto.OrderDto;
import com.study.gamecube.pojo.GameGoods;
import com.study.gamecube.pojo.GameOrder;
import com.study.gamecube.service.GameGoodsService;
import com.study.gamecube.service.GameOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * shop向外暴露的order_api
 * 使用角色：客户[创建订单]，员工[修改订单状态,查询所有订单]
 *
 * @author peng
 */
@RestController
@RequestMapping("/order")
public class GameOrderController {
    /**
     * 调用服务内部的goods表
     */
    @Autowired
    private GameGoodsService gameGoodsService;
    @Autowired
    private GameOrderService gameOrderService;

    /**
     * 创建订单
     *
     * @param orderDto
     * @return
     */
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public GameOrder create(@RequestBody OrderDto orderDto) {
        List<GameGoods> goodsList = gameGoodsService
                .findByIds(orderDto.getGameIdList());
        GameOrder gameOrder = new GameOrder();
        gameOrder.setCustomer(orderDto.getCustomer());
        gameOrder.setItems(goodsList);
        GameOrder order = gameOrderService.createOrder(gameOrder);
        return order;
    }

    /**
     * 根据id查询订单
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public GameOrder getOrder(@PathVariable("id") Long id) {
        GameOrder order = gameOrderService.getById(id);
        return order;
    }

    /**
     * 查询所有订单
     *
     * @return
     */
    @GetMapping
    public R<List<GameOrder>> getAll() {
        List<GameOrder> all = gameOrderService.findAll();
        return R.serialize(all);
    }


}
